package test01;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @author 帅小伙
 * @date 2022/1/2
 * @description
 */
public class Producer2 {

    private static final String QUEUE_NAME = "hello";

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost("47.96.19.90");
        factory.setUsername("mubei");
        factory.setPassword("020323");

        /*
        为什么直接可以关了      connection  和  channel 都实现了AutoCloseable接口
        使用try包裹 可以不用显示调用close()方法
        Next we create a channel, which is where most of the API for getting things done resides.
        Note we can use a try-with-resources statement because both Connection and Channel implement java.io.Closeable.
        This way we don't need to close them explicitly in our code.
        * */
        try( Connection connection = factory.newConnection();  Channel channel = connection.createChannel(); ){
            channel.queueDeclare(QUEUE_NAME,false,false,false,null);
            String msg = "hello world";
            channel.basicPublish("",QUEUE_NAME,null,msg.getBytes());
            System.out.println("消息发送完成");
        }

    }


}
